<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/testharness-adapter.js"></script>
</head>
<body>
<script type="module">
import {GeolocationMock} from './resources/geolocation-mock.js';

description("Tests that when multiple requests are waiting for permission, no callbacks are invoked until permission is denied.");

let error;

const mock = new GeolocationMock();
mock.setGeolocationPosition(51.478, -0.166, 100);

let permissionSet = false;

function denyPermission() {
    permissionSet = true;
    mock.setGeolocationPermission(false);
}

let watchCallbackInvoked = false;
let oneShotCallbackInvoked = false;

navigator.geolocation.watchPosition(function() {
    testFailed('Success callback invoked unexpectedly');
    finishJSTest();
}, function(e) {
    if (permissionSet) {
        error = e;
        assert_equals(error.code, error.PERMISSION_DENIED);
        assert_equals(error.message, "User denied Geolocation");
        watchCallbackInvoked = true;
        maybeFinishTest();
        return;
    }
    testFailed('Error callback invoked unexpectedly');
    finishJSTest();
});

navigator.geolocation.getCurrentPosition(function() {
    testFailed('Success callback invoked unexpectedly');
    finishJSTest();
}, function(e) {
    if (permissionSet) {
        error = e;
        assert_equals(error.code, error.PERMISSION_DENIED);
        assert_equals(error.message, "User denied Geolocation");
        oneShotCallbackInvoked = true;
        maybeFinishTest();
        return;
    }
    testFailed('Error callback invoked unexpectedly');
    finishJSTest();
});
window.setTimeout(denyPermission, 100);

function maybeFinishTest() {
    if (watchCallbackInvoked && oneShotCallbackInvoked)
        finishJSTest();
}
</script>
</body>
</html>
